package misp_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import misp_backend.domain.DishTypeRela;
import misp_backend.domain.Dishes;
import misp_backend.domain.Category;
import misp_backend.domain.Typedetails;
import misp_backend.service.CategoryService;
import misp_backend.service.DishTypeRelaService;
import misp_backend.service.DishesService;
import misp_backend.mapper.DishesMapper;
import misp_backend.service.TypedetailsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yang
 * @description 针对表【dishes】的数据库操作Service实现
 * @createDate 2025-03-19 23:51:58
 */
@Service
public class DishesServiceImpl extends ServiceImpl<DishesMapper, Dishes>
        implements DishesService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishTypeRelaService relaService;
    @Autowired
    private TypedetailsService typedetailsService;

    public List<Dishes> listWithCategoryName() {
        // 获取分类名称映射表
        Map<Long, String> categoryMap = categoryService.list()
                .stream()
                .collect(Collectors.toMap(
                        Category::getCategoryId,
                        Category::getCategoryName,
                        (oldVal, newVal) -> newVal // 处理重复key
                ));

        // 转换菜品数据
        return this.list().stream()
                .map(dish -> {
                    Dishes vo = new Dishes();
                    BeanUtils.copyProperties(dish, vo);
                    vo.setCategoryName(
                            categoryMap.getOrDefault(
                                    dish.getCategoryId(),
                                    "未知分类" // 兜底值
                            )
                    );
                    return vo;
                }).collect(Collectors.toList());
    }

    public List<String> getDishTypeDetail(Long dishid) { //获取菜品所有的口味
        QueryWrapper<DishTypeRela> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", dishid);
        List<DishTypeRela> dishTypeRelaList = relaService.list(queryWrapper);
        List<Integer> items = new ArrayList<>();
        List<String> typesum = new ArrayList<>();
        for (DishTypeRela rela : dishTypeRelaList) {
            items.add(rela.getTypeId());
        }

        for (Integer i : items) {

            QueryWrapper<Typedetails> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("type_id", i);
            List<Typedetails> TypedetailsList = typedetailsService.list(queryWrapper2);
            System.out.println(TypedetailsList);//???
            for (Typedetails ty : TypedetailsList) {
                typesum.add(ty.getTypeValue());
            }
        }

        return typesum;
    }
}




